var next_page=0,page=1;
var index;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    aaa:false,
    class:"",
    statusbar: 0,
    user_id: 0,
    user: {},
    list: [],
    article: {},
    is_detail_show: 0,
    headshow: 0,
    // 消息小红点
    message: 0,
    statusbar: 0,
  },
  zk:function(){
    this.setData({
      aaa:false,
      class:""
    })
  },
  sq:function(){
    this.setData({
      aaa:true,
      class:"aa"
    })
  },
  bianji:function(e){
    console.log(e.currentTarget.dataset.articleId)
    var id=e.currentTarget.dataset.articleId;
  wx.navigateTo({
    url: '/weitao/publish/publish?articleId='   +id   ,
  })

  },



  //展开更多
  toggleHandler: function (e) {
    var that = this;
    console.log(e)
    index = e.currentTarget.dataset.index;
    for (var i = 0; i < that.data.trendsList.length; i++) {
      if (index == i) {
        that.data.trendsList[index].auto = true;
        that.data.trendsList[index].seeMore = false;
      }
    }
    that.setData({
      trendsList: that.data.trendsList
    })
  },
  //收起更多
  toggleContent: function (e) {
    var that = this;
    console.log(e)
    index = e.currentTarget.dataset.index;
    for (var i = 0; i < that.data.trendsList.length; i++) {
      if (index == i) {
        that.data.trendsList[index].auto = true;
        that.data.trendsList[index].seeMore = true;
      }
    }
    that.setData({
      trendsList: that.data.trendsList
    })
  },









//跳转页面
goUrl: function (e) {
  var gopage = e.currentTarget.dataset.url,
    s = this;
  if (!gopage) {
    return;
  }
  console.log(gopage, "去");
  wx.navigateTo({
    url: gopage,
    events: {
      someEvent: function (data) {
        console.log('返回了');
        s.loadData();
      }
    },
    success: function (res) {}
  })
},
loadData: function (t) { console.log('加载')
var a = this;
wx.showShareMenu({
  withShareTicket: true,
  menus: ['shareAppMessage', 'shareTimeline']
})
getApp().core.showLoading({
  title: "加载中"
}), getApp().request({
  url: getApp().api.weitao.index,
  data: {
    city_name: a.data.region.name,
    city_id: a.data.region.id,
    id: a.data.region.id
  },
  success: function (t) {
    console.log(t)
    0 == t.code && a.setData({
      list: t.data.listcont[a.data.pitch],
      user: t.data.user,
      referrer: t.data.referrer,
      ask_answer: t.data.ask_answer,
      listcont: t.data.listcont,
      message: t.data.message,
    });
  },
  complete: function () {
    getApp().core.hideLoading();
  }
});
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (t) {

    const res = wx.getSystemInfoSync()
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
    var statusbarH = res.statusBarHeight
    this.setData({
      statusbar: statusbarH
    })
    getApp().page.onLoad(this, t),page = 1;
    if (t.user_id) {
      this.setData({
        user_id: t.user_id
      });
    }
    this.getUser()
  },
  getUser: function () {
    var t = this
    getApp().core.showLoading({}), getApp().request({
      url: getApp().api.weitao.user_center,
      data: {
        page: page,
        user_id: t.data.user_id
      },
      method: "get",
      success: function (e) {
        getApp().core.hideLoading()
        if (0 == e.code) {
          var i = t.data.list,
            s = e.data.list,
            n = !1;
          10 == s.length && (n = 1), i = i.concat(s), page++, t.setData({
            list: i,
            user: e.data.user
          });
          
          next_page = n;
        } else getApp().core.showToast({
          title: e.msg,
          icon: 'none'
        });
      },
      fali: function (t) {
        getApp().core.hideLoading()
        getApp().core.showToast({
          title: t.msg,
          icon: 'none'
        });
      }
    });
  },
  /**
   * 
   * @param {*} e 
   * 关注
   */
  focus_action(e) {
    var user_id = this.data.user.user_id,
      s = this;
    var like_data = 0;
    s.data.user.is_user_liked == 1 ? like_data = 2 : like_data = 1;
    if (user_id > 0) {
      var a = this;
      getApp().core.showLoading({
        title: "加载中"
      }), getApp().request({
        url: getApp().api.weitao.focus_user,
        data: {
          user_id: user_id,
          is_user_liked: like_data
        },
        success: function (t) {
          0 == t.code && (s.data.user.is_user_liked = like_data) && a.setData({
            user: s.data.user
          });
          console.log(s.data.user);
          getApp().core.showToast({
            title: t.msg,
            icon: 0 == t.code ? 'success' : 'none'
          });
        },
        complete: function () {
          getApp().core.hideLoading();
        }
      });
    } else {
      getApp().core.showToast({
        title: "系统错误",
        icon: 'none'
      });
    }
  },
  share_return() {
    return false;
  },
  like_action(e) {
    var user_index = e.currentTarget.dataset.userIndex,
      s = this,
      article = s.data.list[user_index];
    var like_data = 0;
    article.is_liked == 1 ? like_data = 0 : like_data = 1;
    let like_count = article.like_count;
    if (article.id > 0) {
      var a = this;
      getApp().core.showLoading({
        title: "加载中"
      }), getApp().request({
        url: getApp().api.weitao.like_article,
        data: {
          article_id: article.id,
          is_liked: like_data
        },
        success: function (t) {
          if (t.code == 0) {
            s.data.list[user_index].is_liked = like_data;
            if (like_data == 1) {
              s.data.list[user_index].like_count = parseInt(like_count) + 1;
            } else {
              if (parseInt(like_count) > 1) {
                s.data.list[user_index].like_count = parseInt(like_count) - 1;
              } else {
                s.data.list[user_index].like_count = 0;
              }
            }
            a.setData({
              list: s.data.list
            });
          }
          getApp().core.showToast({
            title: t.msg,
            icon: 'none'
          });
        },
        complete: function () {
          getApp().core.hideLoading();
        }
      });
    } else {
      getApp().core.showToast({
        title: "系统错误",
        icon: 'none'
      });
    }
  },
 
  onUnload(){ 
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('someEvent', 1);
  },
  deleteArticle(e) {
    var article_id = e.currentTarget.dataset.articleId,
      index = e.currentTarget.dataset.index,
      s = this;
    if (article_id > 0) {
      wx.showModal({
        title: '提示',
        content: '是否删除？',
        confirmText: "删除",
        success(res) {
          if (res.confirm) {
            getApp().core.showLoading({
              title: "加载中"
            }), getApp().request({
              url: getApp().api.weitao.delete_article,
              data: {
                article_id: article_id,
              },
              success: function (t) {
                s.data.list.splice(index, 1);
                if (t.code == 0) {
                  s.setData({
                    list: s.data.list
                  });
                  // s.getUser();
                }
                getApp().core.showToast({
                  title: t.msg,
                  icon: 'none'
                });
              },
              complete: function () {
                getApp().core.hideLoading();
              }
            });


          }
        }
      })

    } else {
      getApp().core.showToast({
        title: "系统错误",
        icon: 'none'
      });
    }
  },

  //跳转页面
  goUrl: function (e) {
    var gopage = e.currentTarget.dataset.url;
    if (!gopage) {
      return;
    }
    getApp().core.navigateTo({
      url: gopage
    });
  },
  goBack() {
    wx.navigateBack({
      delta: 1
    })
  },
  show_detail(e) {
    var article = {};
    if (e.currentTarget.dataset.article && this.data.is_detail_show != 1) {
      article = e.currentTarget.dataset.article;
      article.head_url = article.from_user_head;
      article.nickname = article.from_user_name;
      article.is_user_liked = article.is_focused;
    }
    this.setData({
      article: article,
      is_detail_show: this.data.is_detail_show == 1 ? 0 : 1
    });
  },
  onPageScroll: function (e) {
    let opacity = 0
    if (e.scrollTop <= 200) {
      opacity = e.scrollTop / 200
    } else {
      opacity = 1
    }
    this.setData({
      headshow: opacity
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(next_page);
    next_page == 1 && this.getUser()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    getApp().page.onShareAppMessage(this);
    if (res.from === 'button') {
      var article = res.target.dataset.article;
      console.log(article);
      return {
        title: article.content,
        path: '/weitao/details/details?article_id=' + article.id + '&store_id=' + (wx.getStorageSync("store_id") ? wx.getStorageSync("store_id") : 0),
        success: function (res) {
          console.log('成功', res)
        }
      }
    }
    return {
      title: '微淘首页',
      path: '/weitao/index/index',
      success: function (res) {
        console.log('成功', res)
      }
    }
  },
})